<script setup>
import { defineEmits, onMounted, ref } from 'vue';
import chatIcon from '@/assets/svg/chat-icon.svg';
import chatSvg from '@/assets/svg/chat.svg';
import userSvg from '@/assets/svg/user.svg';
import settingSvg from '@/assets/svg/setting.svg';
import moonSvg from '@/assets/svg/moon.svg';
import changeSvg from '@/assets/svg/change.svg'
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/counter';
import { getUserInfo } from '@/api/user';
import robotSvg from '@/assets/svg/robot.svg';

const emit = defineEmits(['switchToChat', 'switchToContacts']);
const router = useRouter()
const isDarkMode = ref(false);
const isMinimized = ref(false);
const userStore = useUserStore();

const navigateToSetting = () => {
  router.push('/setting');
};

const toggleDarkMode = () => {
  isDarkMode.value = !isDarkMode.value;
  document.body.classList.toggle('dark-mode', isDarkMode.value);
  if (isDarkMode.value) {
    document.documentElement.setAttribute('data-theme', 'dark');
    document.documentElement.classList.add('dark');
  } else {
    document.documentElement.removeAttribute('data-theme');
    document.documentElement.classList.remove('dark');
  }
};

const toggleMinimize = () => {
  isMinimized.value = !isMinimized.value;
  const app = document.getElementById('app');
  if (isMinimized.value) {
    app.style.height = '90vh';
    app.style.width = '76vw';
  } else {
    app.style.height = '100vh';
    app.style.width = '100vw';
  }
};

</script>

<template>
  <div class="header">
    <!-- <div class="logo">
      <img :src="chatIcon" alt="Chat Icon" />
    </div> -->
    <div class="user-button">
    <div class="header-one" @click="emit('switchToChat')">
      <img :src="chatSvg" alt="Chat" />
    </div>
    <div class="header-button" @click="emit('switchToContacts')">
      <img :src="userSvg" alt="User" />
    </div>
  </div>
    <div class="search-bar">
      <input type="text" placeholder="Search..." />
    </div>
    <div class="user-settings">
      <div class="changes" @click="toggleMinimize">
        <img :src="changeSvg" alt="Change Icon" />
      </div>
      <div class="dark-light" @click="toggleDarkMode">
        <img :src="moonSvg" alt="Dark Mode Toggle" />
      </div>
      <div class="settings" @click="navigateToSetting">
        <img :src="settingSvg" alt="Setting Icon" />
      </div>

      <img class="user-profile" :src="userStore.userInfo.avatar || robotSvg" alt="account-profile">
    </div>
  </div>
</template>

<style lang="scss" scoped>
.header {
  height: 80px;
  width: 100%;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.logo {
  color: var(--theme-color);
  width: 58px;
  flex-shrink: 0;

  svg {
    width: 100%;
  }
}

.header-one,
.header-button {
  cursor: pointer;
  color: var(--settings-icon-color);
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  img {
    fill: transparent;
    transition: 0.5s;
  }
}

.header-button {
  margin-left: 20px;
}

.search-bar {
  height: 80px;
  z-index: 3;
  position: relative;
  margin-left: 280px;

  input {
    height: 100%;
    width: 100%;
    display: block;
    background-color: transparent;
    border: none;
    color: var(--body-color);
    padding: 0 54px;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 56.966 56.966' fill='%23c1c7cd'%3e%3cpath d='M55.146 51.887L41.588 37.786A22.926 22.926 0 0046.984 23c0-12.682-10.318-23-23-23s-23 10.318-23 23 10.318 23 23 23c4.761 0 9.298-1.436 13.177-4.162l13.661 14.208c.571.593 1.339.92 2.162.92.779 0 1.518-.297 2.079-.837a3.004 3.004 0 00.083-4.242zM23.984 6c9.374 0 17 7.626 17 17s-7.626 17-17 17-17-7.626-17-17 7.626-17 17-17z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-size: 16px;
    background-position: 25px 48%;
    font-family: var(--body-font);
    font-weight: 600;
    font-size: 15px;

    &::placeholder {
      color: var(--input-chat-color);
    }
  }
}

.user-button {
  display: flex;
  align-items: center;
  cursor: pointer;
  flex-shrink: 0;
}

.user-settings {
  display: flex;
  align-items: center;
  cursor: pointer;
  margin-left: auto;
  flex-shrink: 0;

  &>*+* {
    margin-left: 14px;
  }
}

.changes {
  color: var(--settings-icon-color);
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  img {
    width: 100%;
    fill: transparent;
    transition: 0.5s;
  }
}

.settings {
  color: var(--settings-icon-color);
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  img {
    width: 100%;
    fill: transparent;
    transition: 0.5s;
  }
}

.user-profile {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
</style>